<template>
	<view class="page">
		<!-- 搜索 -->
		<view class="search-head">
			<view class="back" @click="onBack">
				<text></text>
			</view>
			<view class="search">
				<text class="iconfont icon-fadajing"></text>
				<input type="text" v-model="keyword" :auto-focus="firstFocus" @input="onInput" placeholder="请输入股票代码 / 名称 / 简称" />
			</view>
			<view class="btn" @click="onCancel">
				<text>取消</text>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="search-record">
			<view class="search-title">
				<view class="title">大家都在搜</view>
				<view class="iconfont icon-laji" @click="onDel()"></view>
			</view>
			<view class="record-list">
				<view class="list" v-for="(item,index) in SearchRecordArr"
				@click="onRecord(item.code)"
				 :key="index">
					<text>{{item.code_title}}</text><text>{{item.code}}</text>
				</view>
			</view>
		</view>
		<!-- 搜索发现 -->
		<view class="search-found">
			<view class="search-title">
				<view class="title">搜索发现</view>
				<!-- <view class="iconfont icon-laji"></view> -->
			</view>
			<view class="found-list">
				<view class="list" v-for="(item,index) in dataList" :key="index">
					<view class="left" @click="onSearch(item.code)">
						<text class="name">{{item.name}}</text><text class="pin">({{item.pin}})</text>
					</view>
					<view class="mid" @click="onSearch(item.code)">
						<text class="code">{{item.code}}</text>
					</view>
					<view class="right" @click="onAttention(item)">
						<text class="iconfont icon-guanzhu-off"></text>
						<!--text>{{ AttentionShow === 0 ? '加入自选' : '取消自选' }}</text-->
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var _self,loginRes;
	export default {
		data() {
			return {
				uid:'',
				token:'',
				AttentionShow: 0,
				SearchRecordArr: [],
				keyword: '',
				dataList:{},
				firstFocus: true
			};
		},
		onLoad() {
			if(uni.getStorageSync('SearchRecordArr')){
				this.SearchRecordArr = JSON.parse(uni.getStorageSync('SearchRecordArr'));
			}else{
				this.getHistory();
			}
		},
		methods:{
			async getHistory(){
				uni.request({
					url: this.apiServer+'/market/index/getHistory_secher',
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'GET',
					timeout: 5000,
					success: res => {
						//console.log(res.data);
						this.SearchRecordArr = res.data.data;
						uni.setStorageSync('SearchRecordArr', JSON.stringify(this.SearchRecordArr));
						console.log(this.SearchRecordArr);
					},
					complete:function(){
					    uni.stopPullDownRefresh();
					},
					fail: (e) => {
						uni.showToast({title:"加载失败!",icon:"none"});
						console.log(e);
					}
				});
			},
			/**
			 * 返回点击
			 */
			onBack(){
				uni.navigateBack();
			},
			/*搜索框输入内容*/
			onInput(e) {
				this.keyword = e.target.value;//监听获取输入框改变值
				if(this.keyword){
					uni.request({
						url: this.apiServer+'/market/index/stock_search?key='+this.keyword,
						header: {'content-type' : "application/x-www-form-urlencoded"},
						method: 'GET',
						timeout: 5000,
						success: res => {
							//console.log(res.data);
							this.dataList = res.data.data;
							console.log(this.dataList);
						},
						complete:function(){
						    uni.stopPullDownRefresh();
						},
						fail: (e) => {
							uni.showToast({title:"加载失败!",icon:"none"});
							console.log(e);
						}
					});
				}else{
					this.dataList = '';
				}
			},
			/*取消*/
			onCancel(){
				this.keyword = "";
				this.dataList= "";
			},
			/**
			 * 搜索点击
			 */
			onSearch(code){
				uni.navigateTo({
					//url: '/pages/GoodsDetails/Candle?code=' + encodeURIComponent(code),
					url: '/pages/MarketDetails/MarketDetails?code=' + encodeURIComponent(code),
				})
			},
			onDel(){
				this.SearchRecordArr = "";
				uni.removeStorageSync('SearchRecordArr');
				uni.showToast({title:"清理成功",icon: "none"});
			},
			/**
			 * 记录点击
			 */
			onRecord(val){
				uni.navigateTo({
					//url: '/pages/GoodsDetails/Candle?code=' + encodeURIComponent(val),
					url: '/pages/MarketDetails/MarketDetails?code=' + encodeURIComponent(val),
				})
			},
			/* 自选点击*/
			onAttention(item) {
				loginRes = this.checkLogin();
				if(!loginRes){return;}
				console.log(loginRes);
				this.uid   = loginRes[0];
				this.token = loginRes[2];
				if (this.AttentionShow === 0) {
					this.AttentionShow = 1;
					uni.request({
						url: this.apiServer+'/market/index/add_my_select',
						header: {'content-type' : "application/x-www-form-urlencoded"},
						method: 'POST',
						timeout: 5000,
						data:{
							uid: this.uid,
							token: this.token,
							name: item.name,
							code: item.code,
						},
						success: res => {
							if(res.data.status == 1){
								uni.showToast({title: res.data.message,icon: 'none'})
							}
						},
						complete:function(){
							uni.stopPullDownRefresh();
						},
						fail:function(e){
							uni.showToast({title:"加载失败!",icon:"none"});
						}
					});
				} else {
					this.AttentionShow = 0;
					uni.request({
						url: this.apiServer+'/market/index/del_my_select',
						header: {'content-type' : "application/x-www-form-urlencoded"},
						method: 'POST',
						timeout: 5000,
						data:{
							token: this.token,
							code: item.code,
						},
						success: res => {
							if(res.data.status == 1){
								uni.showToast({title: res.data.message,icon: 'none'})
							}
							
						},
						complete:function(){
							uni.stopPullDownRefresh();
						},
						fail:function(e){
							uni.showToast({title:"加载失败!",icon:"none"});
						}
					});
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	@import 'search.scss';
</style>
